var comment = liwe.module ( "comment" );

comment._cache = {
			id_obj: '',
			module: '',
			results : []
			};
comment._dest_div = 'block_main';


comment.set_dest_div = function ( dest_div )
{
	commment._dest_div = dest_div;
};

comment.init = function()
{
	console.debug ( "Comment init called ");
	comment.load_templates ();
};

comment.count_result = function() {
	return comment._cache.results.count();
}

comment.render = function ( com_lst, dest_div, buffer )
{
	if ( ! com_lst ) com_lst = comment._cache.results;
	if ( ! dest_div ) dest_div = comment._cache.div_id + '-hold';
	console.debug ( dest_div );
	var res = new String.buffer ();
	com_lst.iterate ( function ( v ) 
			{
				res.add ( String.formatDict ( comment.templates [ 'COM-comment-row' ], v ) );
			} );
	var res_cnt = String.formatDict ( comment.templates [ 'COM-comment-cnt' ], { comment_rows : res, div_id : comment._cache.div_id } );
	if ( buffer ) return res_cnt;
	$( dest_div , res_cnt );
	
};

comment.list = function ( id_obj, module, cback )
{
	dct = {
		'action'  : 'comment.ajax.list',
		'id_obj'  : id_obj,
		'module'  : module,
		'order_by': ''
		}
	comment.ajax ( dct, function ( data ){
						var res = ( data [ 'comments' ] ) ? data [ 'comments' ] : [];
						comment._cache = {
								'id_obj'  : id_obj,
								'module'  : module,
								'results' : res,
								'div_id'  : 'COM-list-' + id_obj + '@' + module
								};
						if ( cback ) cback ();
						});
	
};

comment.add = function ( id_obj, module, txt  )
{
	var dct;
	if ( $ ( 'COM-insert-' + id_obj + '@' + module ) ) 
	{

		var f = liwe.form.get ( 'COM-insert-' + id_obj + '@' + module );
		dct = f.get_values ();
		dct [ 'action' ] = 'comment.ajax.add';

	} else {

		if ( ! id_site ) id_site = site._id_site;
		dct = {
			'action' : 'comment.ajax.add',
			'id_obj' : id_obj,
			'module' : module,
			'txt'    : txt
			};

	}
	/*
	if ( $( 'COM-' + dct.id_obj + '@' + dct.module ) ) 
		{
			$( 'COM-' + dct.id_obj + '@' + dct.module, 'working ...' );
		}
	*/
	comment.ajax ( dct, function ( data ) {
							if ( data.id_comment && data.id_comment > 0 )
							{
								var f = liwe.form.get ( 'COM-insert-' + dct.id_obj + '@' + dct.module );
								f.set_value ( 'txt' , '' );
								comment.list ( f.get_value ( 'id_obj' ), f.get_value ( 'module' ), '', comment.render );
							}
							console.debug ( "--- comment.add : %o" % data );
						} );
};

comment._create_insert_form = function ( id_obj, module )
{
	if ( ! id_obj || ! module ) return;
	var f = new liwe.form.instance ( 'COM-insert-' + id_obj + '@' + module );
	f.hidden ( { 'name' : 'id_obj', 'value' : id_obj } );
	f.hidden ( { 'name' : 'module', 'value' : module } );
	f.hidden ( { 'name' : 'dest_div' } );
	f.textarea ( { 'name' : 'txt' } );
	f.button ( { 'name' : 'Invia', onclick : "comment.add ( " + id_obj + ", '" + module + "' );" } );
	return f.get ();
};

comment.box = function ( id_obj, module, dest_div, buffer )
{
	var dct = {
			'id_obj'   : id_obj,
			'module'   : module,
			'div_id'   : 'COM-' + id_obj + '@' + module, 
			'com_form' : comment._create_insert_form ( id_obj, module )
		  };
	var output = String.formatDict ( comment.templates [ 'COM-comment-box' ], dct );
	if ( buffer ) return output; 
	$( dest_div, output );
};

comment._show_div = function ( target_div, force )
{
        var vis = ''
        if ( force )
        {
                vis = force;
        } else {
                vis = ( $( target_div ).style.display == "none" || ! $( target_div ).style.display ) ? "block" : "none";
        }
        var el_butt = target_div + '-div-butt' ;
        //console.debug ( "--_ show_div:  ( %o ) %s => %s " , $( target_div ), $( target_div ).style.display , vis );
        $( target_div ).style.display = vis;
        if ( $( el_butt ) )
        {
                var butt_class = ( vis == "none" ) ? 'open' : 'close' ;
                $( el_butt ).className = "COM-butt-" + butt_class;
                console.debug ( "--_ show_div: className :  %s" , $( el_butt ).className );
        }
};
